<template>
  <div class="widget_settings_pro footerForm">

    <ele-form-section
        v-model="formData"
        :request-fn="handleSubmit"
        :sections="sections"
        @request-success="handleSuccess"
    >
      <template v-slot:blocks="{ desc, data, field, formData }">
        <el-collapse v-model="activeNames">
          <draggable v-model="formData.templateBlockList" chosenClass="active" animation="500" filter=".forbid" :preventOnFilter="false">
            <template v-for="(item, index) in formData.templateBlockList">
              <el-collapse-item :title="contentMap[item.type]" :name="index" class="game-select editForm" :key="index">
                <div v-if="item.type === 'social'" class="forbid">
                  <div class="form-item form-item-inline">
                    <label>Instagram link</label>
                    <el-switch v-model="item.settings.instagram_url"/>
                  </div>
                  <div class="form-item form-item-inline">
                    <label>Facebook link</label>
                    <el-switch v-model="item.settings.facebook_url"/>
                  </div>
                  <div class="form-item form-item-inline">
                    <label>Twitter link</label>
                    <el-switch v-model="item.settings.twitter_url"/>
                  </div>
                  <div class="form-item form-item-inline">
                    <label>Pinterest link</label>
                    <el-switch v-model="item.settings.pinterest_url"/>
                  </div>
                  <div class="form-item form-item-inline">
                    <label>Youtube link</label>
                    <el-switch v-model="item.settings.youtube_url"/>
                  </div>
                  <div class="form-item">
                  </div>
                </div>
                <div v-if="item.type === 'menu'" class="forbid">
                  <div class="form-item">
                    <label>标题</label>
                    <el-input v-model="item.settings.title"   class="forbid"/>
                  </div>
                  <div class="form-item">
                    <label>选择菜单</label>
                    <div class="form-item inline">
                      <div class="selected">
                        <menu-layout @navMenu='receiveNavMenu($event,index)' :hasAnyMenu="item.settings.menu.id"
                                     :nowMenuName="item.settings.menu.name"></menu-layout>

                      </div>
                    </div>
                  </div>
                </div>
                <div v-if="item.type === 'info'" class="forbid">
                  <div class="form-item">
                    <div class="form-item">
                      <label>标题</label>
                      <el-input v-model="item.settings.title"  class="forbid"/>
                    </div>
                    <label>logo图片</label>
                    <upload-file accept="image/jpeg,image/jpg,image/png"
                                 :fileType="['png', 'jpg', 'jpeg']"
                                 :isShowTip="true"
                                 :lazy="true"
                                 :responseFn="handleResponse"
                                 :action="uploadPath"
                                 title="图片"
                                 v-model="item.settings.logo_src"
                    />
                  </div>
                  <div class="form-item">
                    <label>正文</label>
                    <ele-form-quill-edit style="width:100%;" v-model="item.settings.richtext"></ele-form-quill-edit>
                    <p style="font-size: 12px">{{info}}</p>
                  </div>
                </div>
                <div v-if="item.type === 'newsletter'" class="forbid">
                  <div class="form-item">
                    <label>标题</label>
                    <el-input v-model="item.settings.title"/>
                  </div>
                  订阅用户将被添加到顾客名单
                </div>
                <div class="form-item">
                  <div type="" class="removeBtn" @click="handleRemove(index)"><i class="el-icon-remove"></i>删除内容
                  </div>
                </div>
              </el-collapse-item>
            </template>
          </draggable>
        </el-collapse>
        <el-dropdown trigger="click" v-if="formData.templateBlockList.length!==4" style="width:100%;" placement="top"
                     @command="handleCommand">
          <div class="el-dropdown-link addContent">
            <el-button class="addContent" slot="reference"><i class="el-icon-plus"></i>添加内容</el-button>
          </div>
          <el-dropdown-menu slot="dropdown" style="width:200px;text-align: center;">
            <el-dropdown-item command="1">社交媒体</el-dropdown-item>
            <el-dropdown-item command="2">菜单链接</el-dropdown-item>
            <el-dropdown-item command="3">文字介绍</el-dropdown-item>
            <!--            <el-dropdown-item :disabled="isNewsletter" command="4">邮件订阅<span v-if="isNewsletter">(已添加)</span> </el-dropdown-item>-->
          </el-dropdown-menu>
        </el-dropdown>
      </template>
      <template v-slot:form-btn="{ btns }">
        <div class="rest-btn" @click="reset">
          <svg-icon icon-class="reset-settings" class="icon"></svg-icon>
          重置配置
        </div>
      </template>
    </ele-form-section>
  </div>
</template>

<script>
import draggable from 'vuedraggable';
import uploadFile from '@/components/Upload/uploadFile';
import {debounce, deepClone} from '@/utils/utils';
import MenuLayout from './menuLayout';

const predefineColors = [
    '#ff4500',
    '#ff8c00',
    '#ffd700',
    '#90ee90',
    '#00ced1',
    '#1e90ff',
    '#c71585',
    'rgba(255, 69, 0, 0.68)',
    'rgb(255, 120, 0)',
    'hsv(51, 100, 98)',
    'hsva(120, 40, 94, 0.5)',
    'hsl(181, 100%, 37%)',
    'hsla(209, 100%, 56%, 0.73)'
];

export default {
    name: 'footerForm',
    components: {uploadFile, MenuLayout, draggable},
    props: {
        value: {
            type: Object,
            default: () => ({})
        }
    },
    watch: {
        value: {
            handler (newVal, oldVal) {
                this.formData = JSON.parse(newVal.styleJson);
            },
            immediate: false,
            deep: true
        },
        formData: {
            handler: debounce(function (newVal, oldVal) {
                this.pageCount++;
                if (this.pageCount !== 1) {
                    this.setData();
                }
            }, 1200),
            immediate: false,
            deep: true
        }
    },
    computed: {
        isNewsletter () {
            let value = false;
            this.formData.templateBlockList.forEach(element => {
                if (element.type === 'newsletter') {
                    value = true;
                }
            });
            return value;
        }
    },
    data () {
        return {
            info: '店铺名请使用 {{shopname}}，客服邮箱请使用 {{serviceemail}}，{{ }}用于域名相关店铺信息替换,复制粘贴到文档时请勿修改{{}}中间的内容。',
            dialogTableVisible: false,
            pageCount: 1,
            formData: this.value ? JSON.parse(this.value.styleJson) : {},
            uploadPath: process.env.VUE_APP_BASE_API + '/upload-file',
            activeNames: [],
            contentMap: {
                'social': '社交媒体',
                'menu': '菜单链接',
                'info': '文字介绍',
                'newsletter': '邮件订阅'
            },
            sections: [
                {
                    title: '特殊信息说明',
                    formDesc: {
                        'special_desc': {
                            type: 'ele-form-quill-edit',
                            label: ''
                        }
                    }
                },
                {
                    title: '是否全屏',
                    formDesc: {
                        'is_width_across_screen': {
                            type: 'switch',
                            break: true,
                            label: '宽度铺满屏幕'
                        },
                        'is_width_fixed_width': {
                            type: 'slider',
                            label: () => { return `标题  ${this.formData.is_width_fixed_width}px`; },
                            attrs: {
                                min: 1170,
                                max: 1800
                            }
                        }
                    }
                },
                {
                    title: '订阅',
                    formDesc: {
                        'is_subscription': {
                            type: 'switch',
                            break: true,
                            label: '是否显示订阅'
                        },
                        'subscription_title': {
                            type: 'input',
                            label: '标题'
                        },
                        'subscription_note': {
                            type: 'ele-form-quill-edit',
                            row: 4,
                            label: '内容说明'
                        },
                        'subscription_extend': {
                            type: 'ele-form-quill-edit',
                            row: 4,
                            label: '内容扩展'
                        },
                        'subscription_instagram_url': {
                            type: 'switch',
                            label: 'Instagram link',
                            break: true
                        },
                        'subscription_facebook_url': {
                            type: 'switch',
                            label: 'Facebook link',
                            break: true
                        },
                        'subscription_twitter_url': {
                            type: 'switch',
                            label: 'Twitter link',
                            break: true
                        },
                        'subscription_pinterest_url': {
                            type: 'switch',
                            label: 'Pinterest link',
                            break: true
                        },
                        'subscription_youtube_url': {
                            type: 'switch',
                            label: 'Youtube link',
                            break: true
                        }
                    }
                },
                {
                    title: '颜色',
                    formDesc: {
                        'footer_bg_color': {
                            type: 'color',
                            label: '背景',
                            break: true,
                            attrs: {
                                'show-alpha': true,
                                predefine: predefineColors
                            }
                        },
                        'footer_text_color': {
                            type: 'color',
                            label: '正文',
                            break: true,
                            attrs: {
                                'show-alpha': true,
                                predefine: predefineColors
                            }
                        },
                        'footer_title_color': {
                            type: 'color',
                            label: '标题',
                            break: true,
                            attrs: {
                                'show-alpha': true,
                                predefine: predefineColors
                            }
                        },
                        'footer_payment_icon': {
                            type: 'switch',
                            break: true,
                            label: '显示付款图标'
                        }
                    }
                },
                {
                    title: '内容',
                    formDesc: {
                        blocks: {}
                    }
                }
            ]
        };
    },
    mounted () {
        this.fillData();
        if (!('is_width_across_screen' in this.formData)) {
            this.$set(this.formData, 'is_width_across_screen', false);
        }
        if (!('is_width_fixed_width' in this.formData)) {
            this.$set(this.formData, 'is_width_fixed_width', 1400);
        }
        if (!('is_subscription' in this.formData)) {
            this.$set(this.formData, 'is_subscription', false);
        }
        if (!('subscription_title' in this.formData)) {
            this.$set(this.formData, 'subscription_title', 'KEEP IN TOUCH');
        }
        if (!('subscription_note' in this.formData)) {
            this.$set(this.formData, 'subscription_note', '*By entering your email address or phone number, clicking submit, and completing the sign-up instructions, you consent to receive one or more recurring marketing messages each week. These messages may be sent via an automated system and may include email or text messages, depending on the contact information provided. You also consent to the text terms & privacy policy. Consent is not a condition of purchasing goods or services. To unsubscribe, you can use the unsubscribe link provided in each message or contact our service team for assistance with the unsubscribe process. Please note that standard message and data rates may apply for text messages.');
        }
        if (!('subscription_instagram_url' in this.formData)) {
            this.$set(this.formData, 'subscription_instagram_url', true);
        }
        if (!('subscription_facebook_url' in this.formData)) {
            this.$set(this.formData, 'subscription_facebook_url', true);
        }
        if (!('subscription_twitter_url' in this.formData)) {
            this.$set(this.formData, 'subscription_twitter_url', true);
        }
        if (!('subscription_pinterest_url' in this.formData)) {
            this.$set(this.formData, 'subscription_pinterest_url', true);
        }
        if (!('subscription_youtube_url' in this.formData)) {
            this.$set(this.formData, 'subscription_youtube_url', true);
        }
        if (!('subscription_extend' in this.formData)) {
            this.$set(this.formData, 'subscription_extend', 'Members enjoy FREE SHIPPING on all orders $69 and above  <a href="/login">Join Us Now</a>  or  <a href="/login">Sign In</a>');
        }
    },
    methods: {
        fillData () {
            this.formData.templateBlockList.forEach(item => {
                if (item.type === 'info' && !item.settings.title) {
                    this.$set(item.settings, 'title', 'INFO');
                }
            });
        },
        receiveNavMenu (value, index) {
            this.formData.templateBlockList[index].settings.menu = value;
        },
        addPic () {
            this.formData.blocks.push({
                button_label: '',
                button_link: '',
                enable_image: true,
                image: '',
                text: 'Use this text to describe products,share details on availability and style,or as a space to display recent reviews or FAQs.',
                title: 'Title',
                url: ''
            });
        },

        handleResponse (response, file, fileList) {
            return response;
        },
        handleSubmit (data) {
            console.log(data);
            return Promise.resolve();
        },
        handleSuccess () {
            this.$message.success('创建成功');
        },
        handleCommand (command) {
            let value = {};
            switch (command) {
            case '1':
                value = deepClone(this.formData.defaultBlockList[0]);
                break;
            case '2':
                value = deepClone(this.formData.defaultBlockList[1]);
                break;
            case '3':
                value = deepClone(this.formData.defaultBlockList[2]);
                break;
            case '4':
                value = deepClone(this.formData.defaultBlockList[3]);
                break;
            default:
                break;
            }
            this.formData.templateBlockList.push(value);
        },
        handleRemove (index, record) {
            this.formData.templateBlockList.splice(index, 1);
        },
        reset () {
            this.pageCount = 0;
            this.$emit('resetData', this.value);
        },
        setData: debounce(function () {
            let data = deepClone(this.value);
            data.styleJson = JSON.stringify(this.formData);
            this.$store.commit('SET_PROJECT_DATA', data);
        }, 600)
    }
};
</script>
<style>
.widget_settings_pro.footerForm .el-collapse {
  width: 250px;
}

.footerForm .content {
  margin: 0;
}

.footerForm .el-collapse-item__content {
  padding: 0 10px;
}

.footerForm .editForm .form-item .el-input {
  width: 100% !important;
}

.footerForm.widget_settings_pro .form-item .quill-editor {
  width: auto !important;
}
</style>
<style scoped lang="scss">
.form-item-inline {
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 10px;
  padding-right: 10px;
  color: #606266;
  height: 32px;
  margin-bottom: 10px;
}

.editForm {
  margin-bottom: 10px;

  .form-item {
    padding-top: 10px;

    label {
      display: block;
      height: 32px;
      line-height: 32px;
    }

  }

  ::v-deep .el-collapse-item__content {
    border: 1px solid #d8deec;
    border-top: 0;
    padding-left: 10px;
  }
}

.removeBtn {
  width: 100%;
  display: block;
  text-align: center;
  background-color: #f8f9fb;
  height: 40px;
  line-height: 40px;
  cursor: pointer;
  color: #2e60f8;
}

.addContent {
  width: 100%;
}
</style>
